package com.exoweb.games.hexagone.view
{
    import flash.display.*;
    import flash.events.*;

    import com.exoweb.games.hexagone.view.ImageLocator;

    public class Cell extends Sprite
    {
        public static const CELL_MOVED:String = "CELL_MOVED";
        public static const CELL_ERASED:String = "CELL_ERASED";

        public static const TYPE_AMOUNT:int = 6;
        public static const TYPE_EMPTY:int = -1;

        public static const MOVING_SPEED:Number = 18;
        public static const SWAPPING_SPEED:Number = 6;
        public static const ERASING_SPEED:Number = 0.2;

        public static const WIDTH:Number = 34;
        public static const HEIGHT:Number = 40;
        
        public var col:int;
        public var row:int;
        public var posX:Number;
        public var posY:Number;
        public var type:int;
        public var isMoving:Boolean;
        public var isErase:Boolean;

        private var image:Bitmap;
        private var swapType:int;
        private var swapX:Number;
        private var swapY:Number;

        public function Cell(t:int, c:int, r:int)
        {
            buttonMode = true;
            type = t;
            isErase = false;
            isMoving = false;
            col = c;
            row = r;
            posX = WIDTH * col;
            posY = HEIGHT * row * 0.75;
            x = posX;
            y = posY;
            if((row & 1) == 0)
            {
                posX += 0.5 * WIDTH;
                x = posX;
            }
            setHitArea();
        }

        private function setHitArea():void
        {
            var ha:Sprite = new Sprite();
            ha.graphics.clear();
            ha.graphics.beginFill(0, 0);
            ha.graphics.moveTo(WIDTH/2, HEIGHT/4);
            ha.graphics.lineTo(0, HEIGHT/2);
            ha.graphics.lineTo(-WIDTH/2, HEIGHT/4);
            ha.graphics.lineTo(-WIDTH/2, -HEIGHT/4);
            ha.graphics.lineTo(0, -HEIGHT/2);
            ha.graphics.lineTo(WIDTH/2, -HEIGHT/4);
            ha.graphics.endFill();
            hitArea = ha;
            addChild(ha);
        }

        public function erase():void
        {
            addEventListener(Event.ENTER_FRAME, erasing);
        }

        private function erasing(e:Event):void
        {
            if(scaleX - ERASING_SPEED > 0)
            {
                scaleX -= ERASING_SPEED;
            }
            else
            {
                erased();
            }
        }

        private function erased():void
        {
            removeEventListener(Event.ENTER_FRAME, erasing);
            type = TYPE_EMPTY;
            isErase = false;
            scaleX = 1;
            display();
            dispatchEvent(new Event(CELL_ERASED));
        }

        public function moveTo(cell:Cell):void
        {
            isMoving = true;
            swapType = cell.type;
            swapX = cell.posX;
            swapY = cell.posY;
            addEventListener(Event.ENTER_FRAME, moving);
        }

        private function moving(e:Event):void
        {
            var dx:Number = swapX - posX;
            var dy:Number = swapY - posY;
            var l:Number = Math.sqrt(dx*dx + dy*dy);
            var v:Number = MOVING_SPEED;
            if(type != TYPE_EMPTY && swapType != TYPE_EMPTY)
            {
                v = SWAPPING_SPEED;
            }
            x += v * dx / l;
            y += v * dy / l;
            if((posX - x)*(swapX - x) + (posY - y)*(swapY - y) > 0)
            {
                x = swapX;
                y = swapY;
                moved();
            }
        }

        private function moved():void
        {
            removeEventListener(Event.ENTER_FRAME, moving);
            type = swapType;
            isMoving = false;
            x = posX;
            y = posY;
            display();
            dispatchEvent(new Event(CELL_MOVED));
        }

        public function display():void
        {
            if(image != null)
            {
                removeChild(image);
                image = null;
            }
            if(type != TYPE_EMPTY)
            {
                image = Bitmap(new ImageLocator.cells[type]());
                image.x = -WIDTH/2;
                image.y = -HEIGHT/2;
                addChild(image);
            }
        }
    }
}
